#!/usr/bin/env bash
set -e
set -u
set -o pipefail


source "${BASH_SOURCE%/*}/assertions.sh"


test_get_file_ext() {
    local path=${1:?}
    source "${BASH_SOURCE%/*}/../src/utils/functions.sh"
    get_file_ext "$path"
}


test_get_ext_to_replace_with_html() {
    local file_type=${1:?}
    source "${BASH_SOURCE%/*}/../src/utils/functions.sh"
    get_ext_to_replace_with_html "$file_type"
}


test_get_working_ext() {
    local file_type=${1:?}
    local path=${2:?}
    source "${BASH_SOURCE%/*}/../src/utils/functions.sh"
    get_working_ext "$file_type" "$path"
}


test_get_working_ext_main() {
    assert_equal \
        "test_get_working_ext" \
        "html" \
        "$(test_get_working_ext "markdown" "/foo/bar/glux.md")"
    assert_equal \
        "test_get_working_ext" \
        "html" \
        "$(test_get_working_ext "org" "/foo/bar/glux.org")"
    assert_equal \
        "test_get_working_ext" \
        "txt" \
        "$(test_get_working_ext "org" "/foo/bar/glux.txt")"
    # NOTE: Returns the filename when no ext
    assert_equal \
        "test_get_working_ext" \
        "glux" \
        "$(test_get_working_ext "org" "/foo/bar/glux")"
}


test_get_ext_to_replace_with_html_main() {
    assert_equal \
        "test_get_ext_to_replace_with_html" \
        "md" \
        "$(test_get_ext_to_replace_with_html "markdown")"
    assert_equal \
        "test_get_ext_to_replace_with_html" \
        "org" \
        "$(test_get_ext_to_replace_with_html "org")"

    echo "INFO: Expect output to stderr:"
    assert_not_equal  \
        "test_get_ext_to_replace_with_html" \
        "foo" \
        "$(test_get_ext_to_replace_with_html "foo")"
}


test_get_file_ext_main() {
    assert_equal \
        "test_get_file_ext" \
        "glux" \
        "$(test_get_file_ext "/foo/bar/glux")"
    assert_equal \
        "test_get_file_ext" \
        "md" \
        "$(test_get_file_ext "/foo/bar/glux.md")"
    assert_equal \
        "test_get_file_ext" \
        "foo" \
        "$(test_get_file_ext "/foo/bar/glux.foo")"
}


test_file_extentions_main() {
    echo "${FUNCNAME[0]}"
    test_get_working_ext_main
    test_get_ext_to_replace_with_html_main
    test_get_file_ext_main
}


test_file_extentions_main
